<template>
  <div class="wrapper">
    <header>
      <p>在线支付</p>
    </header>

    <h3>订单信息：</h3>
    <div class="order-info" v-if="orders && orders.business">
      <p>
        {{ orders.business.businessName }}
        <i class="fa fa-caret-down" @click="detailetShow"></i>
      </p>
      <p>¥{{ finalTotal }}</p>
    </div>
    <div v-else>
      <p>订单信息加载中...</p>
    </div>

    <ul class="order-detailet" v-show="isShowDetailet" v-if="orders && orders.list">
      <li v-for="item in orders.list" :key="item.food.foodId">
        <p>{{ item.food.foodName }} x {{ item.quantity }}</p>
        <p>¥{{ item.food.foodPrice * item.quantity }}</p>
      </li>
      <li>
        <p>配送费</p>
        <p>¥{{ orders.business.deliveryPrice }}</p>
      </li>
    </ul>

    <ul class="payment-type">
      <li>
        <img src="../assets/alipay.png" />
        <i class="fa fa-check-circle"></i>
      </li>
      <li>
        <img src="../assets/wechat.png" />
      </li>
    </ul>

    <div class="payment-button">
      <button @click="toIndex">确认支付</button>
    </div>

    <Footer />
  </div>
</template>

<script>
import Footer from "../components/Footer.vue";
import axios from 'axios';

export default {
  name: "Payment",
  data() {
    return {
      orderId: this.$route.query.orderId,
      finalTotal: 0,
      orders: {
        business: {},
        list: [],
      },
      isShowDetailet: false,
    };
  },
  created() {
    if (!this.orderId) {
      alert('订单 ID 不存在');
      return;
    }

    this.$axios.post('OrdersController/getOrdersById', this.$qs.stringify({ orderId: this.orderId }))
      .then(response => {
        this.orders = response.data;
        if (this.orders && this.orders.orderTotal) {
          this.finalTotal = this.orders.orderTotal;
        }
      })
      .catch(error => {
        console.error('获取订单失败:', error);
      });
  },
  methods: {
    detailetShow() {
      this.isShowDetailet = !this.isShowDetailet;
    },
    toIndex() {
      if (!this.orderId) {
        alert('订单ID不存在');
        return;
      }

      this.$axios.post(
        "OrdersController/updateOrderState",
        { orderId: this.orderId, orderState: 1 },
        { headers: { 'Content-Type': 'application/json' } }
      )
      .then(response => {
        if (response.data.success) {
          this.$router.push({ path: '/information' });
        } else {
          alert("支付失败，请重试");
        }
      })
      .catch(() => {
        alert("请求失败，请稍后重试");
      });
    }
  },
  components: { Footer },
};
</script>

<style scoped>
.wrapper {
  width: 100%;
  height: 100%;
}

header {
  width: 100%;
  height: 12vw;
  background-color: #0097FF;
  color: #fff;
  font-size: 4.8vw;
  position: fixed;
  left: 0;
  top: 0;
  z-index: 1000;
  display: flex;
  justify-content: center;
  align-items: center;
}

h3 {
  margin-top: 12vw;
  padding: 4vw 4vw 0;
  font-size: 4vw;
  font-weight: 300;
  color: #999;
}

.order-info {
  padding: 4vw;
  font-size: 4vw;
  color: #666;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.order-info p:last-child {
  color: orangered;
}

.order-detailet li {
  padding: 1vw 4vw;
  display: flex;
  justify-content: space-between;
  font-size: 3vw;
  color: #666;
}

.payment-type li {
  padding: 4vw;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.payment-type li img {
  width: 33vw;
  height: 8.9vw;
}

.payment-type li .fa-check-circle {
  font-size: 5vw;
  color: #38CA73;
}

.payment-button {
  padding: 4vw;
}

.payment-button button {
  width: 100%;
  height: 10vw;
  border: none;
  outline: none;
  border-radius: 4px;
  background-color: #38CA73;
  color: #fff;
}
</style>